package com.test.vote.controller;

import com.test.vote.domain.BattlePlayer;
import com.test.vote.pojo.Battle;
import com.test.vote.pojo.Match;
import com.test.vote.pojo.Player;
import com.test.vote.response.CodeMsg;
import com.test.vote.response.ResultData;
import com.test.vote.service.battleService;
import com.test.vote.service.matchService;
import com.test.vote.service.playerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class matchController {
    @Autowired
    matchService matchService;
    @Autowired
    battleService battleService;
    @Autowired
    playerService playerService;

    //系统管理员查询比赛
    @RequestMapping("/superMatch")
    public String selectMatch(Model model) {
        List<Match> list = matchService.selectMatch();
        model.addAttribute("match", list);
        return "superAdmin/match";
    }

    //比赛管理员查询比赛
    @RequestMapping("/matchMatch")
    public String matchMatch(Model model) {
        List<Match> list = matchService.selectMatch();
        model.addAttribute("match", list);
        return "/matchAdmin/match";
    }

    //用户查询比赛
    @RequestMapping("/userMatch")
    public String userMatch(Model model) {
        List<Match> list = matchService.selectMatch();
        model.addAttribute("match", list);
        return "/user/match";
    }

    //系统管理员添加比赛
    @PostMapping("/addMatch")
    @ResponseBody
    public ResultData addMatch(Match match) {
        if (matchService.addMatch(match) > 0) {
            return ResultData.success(true);
        }
        return ResultData.error(CodeMsg.SERVER_ERROR);
    }

    //添加比赛时查询是否存在该比赛名称
    @GetMapping("/ifMatchName")
    @ResponseBody
    public boolean ifMatchName(Match match) {
        return matchService.ifMatchName(match);
    }

    //管理员根据比赛Id进入维护比赛页面
    @RequestMapping("/battle")
    public String selectBattle(int matchId, Model model) {
        //通过比赛Id把比赛包含所有对战查询
        List<Battle> battle = battleService.selectBattle(matchId);
        //存储所有a选手
        List<Player> playera = new ArrayList<>();
        //存储所有b选手
        List<Player> playerb = new ArrayList<>();
        //循环的把每个对战的a选手找出添加到a选手
        for (int x = 0; x < battle.size(); x++) {
            playera.add(playerService.selectPlayerById(battle.get(x).getPlayeraId()));
        }
        //循环的把每个对战的b选手找出添加到b选手
        for (int x = 0; x < battle.size(); x++) {
            playerb.add(playerService.selectPlayerById(battle.get(x).getPlayerbId()));
        }
        //存储一个对战当中的a和b选手
        BattlePlayer battlePlayer;
        //存储一个比赛当中所有对战的a和b选手
        List<BattlePlayer> battlePlayers = new ArrayList<>();
        //循环的把对战当中的a和b选手存储到battlePlayers
        for (int x = 0; x < battle.size(); x++) {
            battlePlayer = new BattlePlayer();

            battlePlayer.setBattleId(battle.get(x).getBattleId());
            battlePlayer.setMatchId(battle.get(x).getMatchId());

            battlePlayer.setPlayeraImg(playera.get(x).getPlayerImg());
            battlePlayer.setPlayeraName(playera.get(x).getPlayerName());
            battlePlayer.setPlayeraNumber(playera.get(x).getPlayerNumber());
            battlePlayer.setPlayeraSong(battle.get(x).getPlayeraSong());
            battlePlayer.setPlayeraTicket(battle.get(x).getPlayeraTicket());

            battlePlayer.setPlayerbImg(playerb.get(x).getPlayerImg());
            battlePlayer.setPlayerbNumber(playerb.get(x).getPlayerNumber());
            battlePlayer.setPlayerbName(playerb.get(x).getPlayerName());
            battlePlayer.setPlayerbSong(battle.get(x).getPlayerbSong());
            battlePlayer.setPlayerbTicket(battle.get(x).getPlayerbTicket());

            battlePlayer.setBattleStatus(battle.get(x).getBattleStatus());

            battlePlayers.add(battlePlayer);
        }
        //存储一个比赛当中的所有选手
        List<Player> players = playerService.selectPlayer(matchId);
        //存储一个比赛当中的所有没有对战的选手
        List<Player> noBattlePlayers = new ArrayList<>();
        //循环判断对战当中的a选手，b选手和比赛所有选手是否有对战关系
        for (int x = 0; x < players.size(); x++) {
            for (int y = 0; y < battle.size(); y++) {
                if (players.get(x).getPlayerId() == battle.get(y).getPlayeraId() ||
                        players.get(x).getPlayerId() == battle.get(y).getPlayerbId()) {
                    break;
                }
                if (y == battle.size() - 1) {
                    noBattlePlayers.add(players.get(x));
                }
            }
        }
        //存储当前比赛信息
        Match match = matchService.selectMatchById(matchId);
        model.addAttribute("player", players);
        model.addAttribute("battle", battle);
        model.addAttribute("match", match);
        model.addAttribute("noBattlePlayers", noBattlePlayers);
        model.addAttribute("battlePlayers", battlePlayers);
        return "matchAdmin/battle";
    }

    //用户根据比赛d进入选择对战投票页面
    @RequestMapping("/userBattle")
    public String userBattle(int matchId, Model model) {
        //通过比赛Id把比赛包含所有对战查询
        List<Battle> battle = battleService.selectBattle(matchId);
        //存储所有a选手
        List<Player> playera = new ArrayList<>();
        //存储所有b选手
        List<Player> playerb = new ArrayList<>();
        //循环的把每个对战的a选手找出添加到a选手
        for (int x = 0; x < battle.size(); x++) {
            playera.add(playerService.selectPlayerById(battle.get(x).getPlayeraId()));
        }
        //循环的把每个对战的b选手找出添加到b选手
        for (int x = 0; x < battle.size(); x++) {
            playerb.add(playerService.selectPlayerById(battle.get(x).getPlayerbId()));
        }
        //存储一个对战当中的a和b选手
        BattlePlayer battlePlayer;
        //存储一个比赛当中所有对战的a和b选手
        List<BattlePlayer> battlePlayers = new ArrayList<>();
        //循环的把对战当中的a和b选手存储到battlePlayers
        for (int x = 0; x < battle.size(); x++) {
            battlePlayer = new BattlePlayer();

            battlePlayer.setBattleId(battle.get(x).getBattleId());
            battlePlayer.setMatchId(battle.get(x).getMatchId());

            battlePlayer.setPlayeraImg(playera.get(x).getPlayerImg());
            battlePlayer.setPlayeraName(playera.get(x).getPlayerName());
            battlePlayer.setPlayeraNumber(playera.get(x).getPlayerNumber());
            battlePlayer.setPlayeraSong(battle.get(x).getPlayeraSong());
            battlePlayer.setPlayeraTicket(battle.get(x).getPlayeraTicket());

            battlePlayer.setPlayerbImg(playerb.get(x).getPlayerImg());
            battlePlayer.setPlayerbNumber(playerb.get(x).getPlayerNumber());
            battlePlayer.setPlayerbName(playerb.get(x).getPlayerName());
            battlePlayer.setPlayerbSong(battle.get(x).getPlayerbSong());
            battlePlayer.setPlayerbTicket(battle.get(x).getPlayerbTicket());

            battlePlayer.setBattleStatus(battle.get(x).getBattleStatus());

            battlePlayers.add(battlePlayer);
        }
        //存储一个比赛当中的所有选手
        List<Player> players = playerService.selectPlayer(matchId);
        //存储当前比赛的信息
        Match match = matchService.selectMatchById(matchId);
        model.addAttribute("player", players);
        model.addAttribute("battle", battle);
        model.addAttribute("match", match);
        model.addAttribute("playera", playera);
        model.addAttribute("playerb", playerb);
        model.addAttribute("battlePlayers", battlePlayers);
        return "user/battle";
    }

}
